#ifndef ImageToBlobFilter_cxx
#define ImageToBlobFilter_cxx

#include "ImageToBlobFilter.h"


namespace FPTK{

template<class PixelType,unsigned int Dimension>
ImageToBlobFilter<PixelType,Dimension>::ImageToBlobFilter()
{
blob=NULL;
image=NULL;
filename=NULL;
for(int i=0;i<Dimension;i++)
   {
   size[i]=0;
   }
}

template<class PixelType,unsigned int Dimension>
int ImageToBlobFilter<PixelType,Dimension>::SetName(char *_filename)
{
delete []filename;
filename=new char[strlen(_filename)+1];
if(strcpy(filename,_filename))
   {
   return 0;
   }
else
   {
   return -1;
   }
}


template<class PixelType,unsigned int Dimension>
char* ImageToBlobFilter<PixelType,Dimension>::GetName()
{
return filename;
}



template<class PixelType,unsigned int Dimension>
ImageToBlobFilter<PixelType,Dimension>::ImageToBlobFilter(ImageTypePointer _image)
{
image=_image;
size=image->GetLargestPossibleRegion().GetSize();
}


template<class PixelType,unsigned int Dimension>
ImageToBlobFilter<PixelType,Dimension>::~ImageToBlobFilter()
{
}


template<class PixelType,unsigned int Dimension>
int ImageToBlobFilter<PixelType,Dimension>::SetInput(ImageTypePointer _image)
{
image=_image;
size=image->GetLargestPossibleRegion().GetSize();
return 0;
}



template<class PixelType,unsigned int Dimension>
int ImageToBlobFilter<PixelType,Dimension>::Update()
{
typedef itk::ImageRegionIteratorWithIndex< ImageType> IteratorType;
BlobType::PointListType list;
ImageType::IndexType pixelIndex;
IteratorType itimage(image,image->GetLargestPossibleRegion());

for(itimage.GoToBegin();!itimage.IsAtEnd();++itimage)
   {
   pixelIndex=itimage.GetIndex();
   BlobPointType point;
   if(itimage.Get()>0)
      {
      point.SetPosition(pixelIndex[0],pixelIndex[1],pixelIndex[2]);
      point.SetRed(image->GetPixel(pixelIndex));
      point.SetGreen(image->GetPixel(pixelIndex));
      point.SetBlue(image->GetPixel(pixelIndex));
      point.SetAlpha(1.0);
      list.push_back(point);
      }
   }


vector vecteur=image->GetSpacing();
double *tab=new double[Dimension];
for(int i=0;i<Dimension;i++)
   {
   tab[i]=vecteur[i];
   }
blob = BlobType::New();
if(filename==NULL)
   {
   blob->GetProperty()->SetName("My Blob");
   }
else
   {
   blob->GetProperty()->SetName(filename);
   }
blob->SetId(1);
blob->SetSpacing(tab);
blob->SetPoints(list);
return 0;
}


template<class PixelType,unsigned int Dimension>
typename ImageToBlobFilter<PixelType,Dimension>::BlobPointer ImageToBlobFilter<PixelType,Dimension>::GetBlob()
{
return blob;
}

}//end of FPTK namespace

#endif